package com.huluobo.a2208mdmdemo2

import androidx.compose.foundation.Image
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController

/**
 *  Created by LC on 2025/2/5.
 */
sealed class NavItem(val route: String, val title: String, val img: Int) {
    data object messagePage : NavItem("message", "消息", R.drawable.icon1275)
    data object homePage : NavItem("home", "门户", R.drawable.icon64)
    data object minePage : NavItem("mine", "我的", R.drawable.icon64)
}

@Composable
fun MainPage() {
    val list = listOf<NavItem>(NavItem.messagePage, NavItem.homePage, NavItem.minePage)
    val navController = rememberNavController()
    //获取当前回退栈中所有的页面状态
    val stackEntryState by navController.currentBackStackEntryAsState()
    //拿到回退栈中的导航地址
    val destination = stackEntryState?.destination

    Scaffold(bottomBar = {
        if (destination?.hierarchy?.any {
                list.map { navItem -> navItem.route }.contains(it.route)
            } == true) {
            NavigationBar {
                list.forEach {
                    NavigationBarItem(selected = false, onClick = {
                        navController.navigate(it.route)
                    }, icon = {
                        Image(painter = painterResource(id = it.img), contentDescription = "")
                    }, label = {
                        Text(text = it.title)
                    })
                }
            }
        }
    }) {
        MyNavHost(it = it, navController = navController)
    }
}